package com.xiaohudie.SGGJUC;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @Author xiao hu die
 * @Date 2022/3/8 13:13
 * @Version 1.0
 */
/**就是线程中的加法计数器*/
public class TestCyclicBarrier {
 public static void main(String[] args) {
  CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
   System.out.println("到镇上了");
  });
  for (int i = 0; i < 7; i++) {
   final  int a =i;
   new Thread(()->{
    System.out.println(Thread.currentThread().getName()+"走了一公里");
    try {
     cyclicBarrier.await();
    } catch (InterruptedException e) {
     e.printStackTrace();
    } catch (BrokenBarrierException e) {
     e.printStackTrace();
    }
   },String.valueOf(i)).start();
  }
 }
}
